package coollemon.view;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
public class PhotoPane extends JPanel implements MouseMotionListener,MouseListener
{
	BufferedImage image = null;
	int zoom = 50;
	int w = 128,h = 128;
	int x = 0, y = 0;
	int usedX, usedY;
	PhotoPane()
	{
		this.setLayout(null);
		this.setPreferredSize(new Dimension(800,800));
		image = null;
	}
	public void paint(Graphics g)
	{
		g.clearRect(0, 0, this.getWidth(), this.getHeight());
		if(image != null)
		{
			g.drawImage(image, 0, 0, (int)(image.getWidth(this)*Math.pow((double)4, ((double)zoom-50)/50)),
					(int)(image.getHeight(this)*Math.pow((double)4, ((double)zoom-50)/50)), Color.white, this);
		}
		g.setColor(Color.black);
		g.drawRect(x, y, w, h);
		Graphics2D gd = (Graphics2D)g;
		gd.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.2f));
		g.setColor(Color.blue);
		g.fillRect(x, y, w, h);
	}
	public void setSize()
	{
		int forx1,forx2;
		forx1 = this.getPreferredSize().width;
		this.setPreferredSize(new Dimension((int)(image.getWidth(null)*Math.pow((double)4, ((double)zoom-50)/50)),
				(int)(image.getHeight(null)*Math.pow((double)4, ((double)zoom-50)/50))));
		forx2 = this.getPreferredSize().width;
		this.x = x*forx2/forx1;
		this.y = y*forx2/forx1;
	}
	public void openfile(File inFile)
	{
		try {
			image = ImageIO.read(inFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(image != null)
		{
			this.x = 0;
			this.y = 0;
			this.setSize();
			this.repaint();
		}
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		int mouseX = e.getX(), mouseY = e.getY();
		if(this.getCursor().getType() == Cursor.MOVE_CURSOR)
		{
			x += (mouseX - usedX);
			y += (mouseY - usedY);
			usedX = mouseX;
			usedY = mouseY;
		}
		if(x > this.getWidth() - w){x = this.getWidth() - w;}
		if(y > this.getHeight() - h){y = this.getHeight() - h;}
		if(x<0){x = 0;}
		if(y<0){y = 0;}
		this.repaint();
		if(this.getCursor().getType() != Cursor.MOVE_CURSOR)
		{
			int barX = PhotoScrollPane.getInstance().getHorizontalScrollBar().getValue();
			int barY = PhotoScrollPane.getInstance().getVerticalScrollBar().getValue();
			PhotoScrollPane.getInstance().getHorizontalScrollBar().setValue(barX -= (mouseX - usedX));
			PhotoScrollPane.getInstance().getVerticalScrollBar().setValue(barY -= (mouseY - usedY));
			usedX = mouseX;
			usedY = mouseY;
		}
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		int mouseX = e.getX(), mouseY = e.getY();
		if((mouseX - x < w)&&(mouseY - y < h)&&(mouseX - x > 0)&&(mouseY - y > 0))
		{
			setCursor(new Cursor(Cursor.MOVE_CURSOR));
		}
		else
		{
			setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		}
		
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		usedX = e.getX();
		usedY = e.getY();
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
